package com.java.kettle;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Properties;

public class AESKeyGenerator {

    public static void main(String[] args) {
        try {
            // 生成 AES 256位密钥
            KeyGenerator keyGen = KeyGenerator.getInstance("AES");
            keyGen.init(256); // 设置密钥长度为256位
            SecretKey secretKey = keyGen.generateKey();

            // 将密钥转换为Base64编码字符串
            String encodedKey = Base64.getEncoder().encodeToString(secretKey.getEncoded());

            // 写入符合Kettle要求的INI格式文件
            try (FileWriter writer = new FileWriter("kettle_aes_key.ini")) {
                writer.write("[AES]\n");
                writer.write("key=" + encodedKey + "\n");
            }

            System.out.println("Kettle AES key file generated successfully: kettle_aes_key.ini");
        } catch (NoSuchAlgorithmException e) {
            System.err.println("AES algorithm not available: " + e.getMessage());
        } catch (Exception e) {
            System.err.println("Error generating key file: " + e.getMessage());
        }
    }

}
